<?php

namespace Admin\Controller;

use Admin\Controller\CommonController;
use Usap\Service\Service;

/**
 *运维中心
 */
class OperationController extends CommonController
{

    /*巡检任务*/
    public function checkcenter($page = 1, $rows = 20, $order = 'a.id', $sort = 'desc')
    {
        $crontal_service = Service::instance()->find('common', 'crontal');//任务接口
        $user_service = Service::instance()->find('account', 'user');
        $user_list = $user_service->getUserList();
        $status_config = Service::instance()->loadConfig('common', 'common', 'getTaskStatusConf');
        if (IS_POST) {
            $request = I('request.');
            $where = 1;
            if (!empty($request['start_date'])) {
                $start_date = $request['start_date'];
                $where .= " and a.begin_time >= '$start_date'";
            }
            if (!empty($request['end_date'])) {
                $end_date = $request['end_date'];
                $where .= " and a.end_time <= '$end_date'";
            }
            if ($request['keyword']) {
                $keyword = $request['keyword'];
                $key_type = $request['key_type'];
                if ($key_type == 1) {
                    $where .= " and a.code like '%$keyword%'";
                } elseif ($key_type == 2) {
                    $where .= " and c.user_name like '%$keyword%'";
                } else {
                    $where .= " and a.code like '%$keyword%' or c.user_name like '%$keyword%' or a.taskName like '%$keyword%'";
                }
            }
            $cwhere = '';
            if ($request['no_assign'] == 1) {
                if (!empty($cwhere)) {
                    $cwhere .= " or a.status = 0 ";
                } else {
                    $cwhere .= " a.status = 0 ";
                }

            }
            if ($request['no_executed'] == 1) { //待执行
                if (!empty($cwhere)) {
                    $cwhere .= " or a.status = 1";
                } else {
                    $cwhere .= " a.status = 1 ";
                }
            }
            if ($request['executed'] == 1) {
                if (!empty($cwhere)) {
                    $cwhere .= " or a.status = 2";
                } else {
                    $cwhere .= " a.status = 2 ";
                }
            }
            if ($request['ignore'] == 1) {
                if (!empty($cwhere)) {
                    $cwhere .= " or a.status =3";
                } else {
                    $cwhere .= " a.status =3";
                }
            }
            if (!empty($cwhere)) {
                $where .= " and ( $cwhere )";
            }
            $checkTask = $crontal_service->getCheckTaskList($where, $page, $rows, $order, $sort, 'a.*,b.codeserial,b.device_category,c.user_name');
            $list = [];
            if ($checkTask['count'] > 0) {
                foreach ($checkTask['list'] as $value) {
                    $res['id'] = $value['id'];
                    $res['code'] = $value['code']; //任务编号
                    $res['name'] = $value['taskName'];
                    $res['start_date'] = $value['begin_time'];
                    $res['stop_date'] = $value['end_time'];
                    $res['user'] = !empty($value['user_id']) ? $user_list[$value['user_id']] : '未分配';
                    $res['state'] = $status_config[$value['status']];
                    $list[] = $res;
                }
            }
            $data = array('total' => $checkTask['count'], 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->display('checkcenter');
        }
    }

    //忽略巡检任务
    public function updateCheckTask()
    {
        $crontal_service = Service::instance()->find('common', 'crontal');//任务接口
        $id = I('request.id');
        if (!is_numeric($id)) {
            $id = explode(',', $id);
        }
        $data = [
            'status' => 3
        ];
        $res = false;
        if (is_array($id)) {
            foreach ($id as $item) {
                $checktaskinfo = $crontal_service->getCheckTaskInfoById($item);
                $device_id = $checktaskinfo['device_id'];
                $res = $crontal_service->updateCheckTaskInfo($item, $data, $device_id);
            }
        } else {
            $checktaskinfo = $crontal_service->getCheckTaskInfoById($id);
            $device_id = $checktaskinfo['device_id'];
            $res = $crontal_service->updateCheckTaskInfo($id, $data, $device_id);
        }
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('系统错误');
        }
    }


    /*
     * 巡检配置
     * */
    public function checkconf($page = 1, $rows = 20, $order = 'id', $sort = 'asc')
    {
        $crontal_service = Service::instance()->find('common', 'crontal');//任务接口
        $area_service = Service::instance()->find('operation', 'area');
        $devices_service = Service::instance()->find('devices', 'devices');
        $palce_service = Service::instance()->find('operation', 'place');
        $area_list = $area_service->get_area_config();
        $where = [];
        $checkTask = $crontal_service->getCheckTaskList($where, $page, $rows, $order, $sort);
        $list = [];
        if ($checkTask['count'] > 0) {
            foreach ($checkTask['list'] as $value) {
                $device_id = $value['device_id'];
                $device_info = $devices_service->getInfoByDeviceId($device_id);
                $place_id = $device_info['place_id'];
                $place_info = $palce_service->getPlaceInfoById($place_id);
                $res['id'] = $device_id;
                $res['cycle'] = $device_info['cycle'];
                $res['area'] = $area_list[$place_info['area_id']];
                $res['code'] = $device_info['codeserial'];
                $list[] = $res;
            }
        }
        $data = array('total' => $checkTask['count'], 'rows' => $list);
        $this->ajaxReturn($data);
    }

    /**
     * 巡检配置编辑
     */
    public function checkEdit()
    {
        $crontal_service = Service::instance()->find('common', 'crontal');//任务接口
        $devices_service = Service::instance()->find('devices', 'devices');
        $check_cycle_config = Service::instance()->loadConfig('Common', 'device', 'getCheckCycle');
        $id = I('request.id');
        $where = [
            'status' => 0,
            'device_id' => $id,
        ];
        $checkTask = $crontal_service->getCheckTaskList($where);
        $device_info = $devices_service->getInfoByDeviceId($id);
        $task_info = $checkTask['list'][0];
        if (IS_POST) {
            $request = I('request.');

        } else {
            $this->assign('device_info', $device_info);
            $this->assign('task_info', $task_info);
            $this->assign('check_cycle_config', $check_cycle_config);
            $this->display('check_edit');
        }

    }


    /*
     * 分配人员
     */
    public function to_user()
    {
        $id = I('request.id');
        $user_service = Service::instance()->find('account', 'user');
        $user_list = $user_service->getUserList();
        $crontal_service = Service::instance()->find('common', 'crontal');//任务接口
        $crontal_info = $crontal_service->getCheckTaskInfoById($id);
        if (IS_POST) {
            $request = I('request.');
            $id = $request['id'];
            $data = [
                'user_id' => $request['user_id'],
                'status' => '1',
                'permission_time' => date('Y-m-d H:i:s')
            ];
            $res = $crontal_service->updateCheckTaskInfo($id, $data);
            if ($res) {
                $data = ['code' => '0000', 'info' => '操作成功'];
                $this->ajaxReturn($data);
            }
        } else {
            $this->assign('crontal_info', $crontal_info);
            $this->assign('user_list', $user_list);
            $this->display('to_user');
        }

    }


    /**
     * 区域管理
     */
    public function areas_list()
    {
        $admin_db = D('Usap/Admin/Admin');//地区管理员
        $operation_service = Service::instance()->find('operation', 'operation');
        $area_service = Service::instance()->find('operation', 'area');
        //获取父级区域配置
        $manager_list = $admin_db->where(['roleid' => 3])->getField('userid,username');
        if (IS_POST) {
            $request = I('request.');
            $where = 1;
            if ($request['keyword']) {
                $keyword = $request['keyword'];
                $where .= " and name like '%$keyword%'";
            }
            $list = [];
            $areas_list = $operation_service->get_areas_list($where);
            if ($areas_list['count'] > 0) {
                foreach ($areas_list['list'] as $value) {
                    if ($value['parent_id']) {
                        $parent_info = $area_service->getInfoById($value['parent_id']);
                    }
                    $value['parent_id'] = $parent_info['name'];
                    $list[] = $value;
                }
            }
            $data = array('total' => $areas_list['count'], 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->assign('manager_list', $manager_list);
            $this->display('areas_list');
        }

    }

    /**
     * 删除区域
     */
    public function delete_area()
    {
        $id = I('request.id');
        $area_service = Service::instance()->find('operation', 'area');
        $area_info = $area_service->getInfoById($id);
        $parent = [];
        if ($area_info) {
            $parent = $area_service->getInfoByParentId($area_info['area_id']);
        }
        //查看是否绑定场所或者单位
        $companies_service = Service::instance()->find('operation', 'companies');//单位接口
        $place_service = Service::instance()->find('operation', 'place');//场所接口
        $companies_info = $companies_service->getCompaniesByCondition(array('area_id' => $id));
        if ($companies_info) {
            $this->error('存在绑定单位，删除失败');
        }
        $place_info = $place_service->getPlaceByCondition(array('area_id' => $id));
        if ($place_info) {
            $this->error('存在绑定场所，删除失败');
        }
        if (!$parent) {
            $result = $area_service->deletArea($id);
            if ($result['code'] == '0000') {
                $this->success('删除成功');
            } else {
                $this->ajaxReturn($result);
            }
        } else {
            $this->error('存在下级区域，删除失败');
        }
    }


    /**
     *区域添加/编辑
     */
    public function set_areas()
    {
        $area_service = Service::instance()->find('operation', 'area');
        $fcount = 0;
        $tcount = 0;
        $flist = $area_service->getListByLevel(1);//一级区域
        if ($flist) {
            $fcount = count($flist);
        }
        $tlist = $area_service->getListByLevel(2);//二级区域
        if ($tlist) {
            $tcount = count($tlist);
        }
        //获取父级区域配置
        $request = I('request.');
        $admin_db = D('Usap/Admin/Admin');//地区管理员
        $manager_list = $admin_db->where(['roleid' => 3])->getField('userid,username');
        $id = $request['id'];
        $manage_info = $area_service->getAreaManageInfo($id);
        $areas_info = $area_service->getInfoById($id);
        if ($areas_info['parent_id']) {
            $areas_info['parent_name'] = $area_service->getInfoById($areas_info['parent_id'])['name'];
        }
        if ($manage_info) {
            foreach ($manage_info as $kk => $vv) {
                $vv['username'] = $manager_list[$vv['userid']];
                $manage_info[$kk] = $vv;
            }
        }
        if (IS_POST) {
            $request = I('request.');
            $manage_data = [];
            $data = [
                'name' => $request['name'],
                'parent_id' => $request['parent_id'],
                'chk' => $request['type'],
                'updated_time' => time(),
            ];
            if ($id) {
                if ($request['manange']) {
                    $i = 0;
                    foreach ($request['manange'] as $key => $value) {
                        foreach ($manager_list as $k => $v) {
                            if ($v == $key) {
                                $manage_data[$i]['userid'] = $k;
                            }
                        }
                        $manage_data[$i]['area_id'] = $id;
                        if (isset($value['call']) && $value['call'] == 'on') {
                            $manage_data[$i]['is_open_call'] = 1;
                        } else {
                            $manage_data[$i]['is_open_call'] = 0;
                        }
                        if (isset($value['msg']) && $value['msg'] == 'on') {
                            $manage_data[$i]['is_open_msg'] = 1;
                        } else {
                            $manage_data[$i]['is_open_msg'] = 0;
                        }
                        if (isset($value['push']) && $value['push'] == 'on') {
                            $manage_data[$i]['is_open_push'] = 1;
                        } else {
                            $manage_data[$i]['is_open_push'] = 0;
                        }
                        $i++;
                    }
                }
                if (!empty($manage_data)) {
                    $area_service->deletAreaManage($id);
                    $area_service->addAreaManage($manage_data);
                }
                $res = $area_service->updateArea($id, $data);

            } else {
                $res = $area_service->addAreas($data);
                if ($request['manange']) {
                    $i = 0;
                    foreach ($request['manange'] as $key => $value) {
                        foreach ($manager_list as $k => $v) {
                            if ($v == $key) {
                                $manage_data[$i]['userid'] = $k;
                            }
                        }
                        $manage_data[$i]['area_id'] = $res;
                        if (isset($value['call']) && $value['call'] == 'on') {
                            $manage_data[$i]['is_open_call'] = 1;
                        }
                        if (isset($value['msg']) && $value['msg'] == 'on') {
                            $manage_data[$i]['is_open_msg'] = 1;
                        }
                        if (isset($value['push']) && $value['push'] == 'on') {
                            $manage_data[$i]['is_open_push'] = 1;
                        }
                        $i++;
                    }
                }
                if (!empty($manage_data)) {
                    $area_service->addAreaManage($manage_data);
                }
            }
            if ($res != false) {
                $data = ['code' => '0000', 'info' => '操作成功'];
                $this->ajaxReturn($data);
            }

        } else {

            $this->assign('manager_list', $manager_list);
            $this->assign('manage_info', $manage_info);
            $this->assign('areas_info', $areas_info);
            $this->assign('fcount', $fcount);
            $this->assign('tcount', $tcount);
            $this->display('set_areas');
        }

    }

    /**
     * 获取对应层级区域列表
     */
    public function get_level_for_area()
    {
        $area_service = Service::instance()->find('operation', 'area');
        $request = I('request.');
        $level = $request['level'] ? $request['level'] : 1;
        if (!$level) {
            $this->error('参数错误');
        }
        $list = $area_service->getListByLevel($level);
        $info = [];
        if ($list) {
            foreach ($list as $key => $value) {
                $res['id'] = $key;
                $res['name'] = $value;
                $info[] = $res;
            }
        }
        $this->ajaxReturn($info);
    }

    /**
     * 单位管理
     * 单位管理员 roleid =5 与数据库对应上
     */
    public function cp_manage()
    {

        $companies_service = Service::instance()->find('operation', 'companies');
        $area_service = Service::instance()->find('operation', 'area');
        $areas_list = $area_service->get_area_config();
        if (IS_POST) {
            $request = I('request.');
            $where = 1;
            if ($request['keyword']) {
                $keyword = $request['keyword'];
                $where .= " and name like '%$keyword%' or address like '%$keyword%' or district like '%$keyword%' or subdistrict like '%$keyword%' or ucc like '%$keyword%' or principal like '%$keyword%' or phone like '%$keyword%'";
            }
            $cp_list = $companies_service->get_companies_list($where);
            $list = [];
            if ($cp_list['count'] > 0) {
                foreach ($cp_list['list'] as $value) {
                    $value['area_id'] = $areas_list[$value['area_id']];
                    $list[] = $value;
                }
            }
            $data = array('total' => $cp_list['count'], 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->display('cp_manage');
        }

    }

    /**
     * 设置单位信息
     */
    public function set_cp_info()
    {

        $admin_db = D('Usap/Admin/Admin');
        //单位管理员
        $manager_list = $admin_db->where(['roleid' => 5])->getField('userid,username');
        $area_service = Service::instance()->find('operation', 'area');//区域接口
        $companies_service = Service::instance()->find('operation', 'companies');//单位接口
        //获取区域列表
        $areas_list = $area_service->get_area_config();
        //负责人
        $id = I('request.id');
        $companies = $companies_service->getCompaniesInfoById($id);
        if (!empty($companies)) {
            $arr = explode('/', $companies['district']);
            unset($companies['district']);
            $companies['district']['province'] = $arr[0];
            $companies['district']['city'] = $arr[1];
            $companies['district']['county'] = $arr[2];
        } else {
            $companies['district']['province'] = '省';
            $companies['district']['city'] = '市';
            $companies['district']['county'] = '地区';
        }
        if (IS_POST) {
            $request = I('request.');
            $id = $request['id'] ? $request['id'] : '';
            $district = '';
            if (isset($request['district']) && $request['district']) {
                $arr = [];
                foreach ($request['district'] as $v) {
                    if ($v) {
                        $arr[] = $v;
                    }
                }
                $district = implode('/', $arr);
            }
            if (!$id) {
                $cid = $companies_service->get_companies_id();//获取主键
                $code = '100' . ($cid + 1);
            } else {
                $code = '100' . $id;
            }
            $data = [
                'area_id' => $request['area_id'],//区域ID
                'name' => $request['name'],//单位名称
                'code' => $code,//单位编号
                'userid' => $request['userid'],//单位名称
                'address' => $request['address'],//单位地址
                'district' => $district,//行政区域
                'subdistrict' => $request['subdistrict'],//街道办
                'ucc' => $request['ucc'],//社会信用码
                'principal' => $request['principal'],//负责人
                'phone' => $request['phone'],//手机号码
                'created_time' => date('Y-m-d H:i:s')
            ];
            if ($id) {
                $res = $companies_service->update_companies_info($id, $data);
            } else {
                $res = $companies_service->add_companies_info($data);
            }
            if ($res) {
                $data = ['code' => '0000', 'info' => '操作成功'];
            } else {
                $data = ['code' => '-10000', 'info' => '系统错误'];
            }
            $this->ajaxReturn($data);
        } else {
            $this->assign('manager_list', $manager_list);
            $this->assign('areas_list', $areas_list);
            $this->assign('companies', $companies);
            $this->display('set_cp_info');
        }
    }

    /**
     * 删除单位
     */
    public function delete_cp_info()
    {
        $id = I('request.id');
        $companies_service = Service::instance()->find('operation', 'companies');//单位接口
        $place_service = Service::instance()->find('operation', 'place');
        if (!is_numeric($id)) {
            $id = explode(',', $id);
        }
        if (is_array($id)) {
            $msg = '';
            foreach ($id as $v) {
                $condition = "company_id = '$v'";
                $companies_info = $companies_service->getCompaniesInfoById($v);
                $place_info = $place_service->getPlaceByCondition($condition);
                if ($place_info) {
                    $msg .= $companies_info['name'] . '-';
                    continue;
                }
                $companies_service->deleteCompanies($v);
            }
            if ($msg) {
                $msg .= '删除失败,存在下级场所';
            }
            $this->success('删除成功，' . $msg);
        } else {
            $companies_info = $companies_service->getCompaniesInfoById($id);
            $condition = "company_id = '$id'";
            $place_info = $place_service->getPlaceByCondition($condition);
            if ($place_info) {
                $this->error('删除失败，存在下级场所');
            }
            if ($companies_info) {
                $res = $companies_service->deleteCompanies($id);
                if ($res) {
                    $this->success('删除成功');
                } else {
                    $this->error('系统错误，请重试');
                }
            } else {
                $this->error('系统错误，请重试');
            }
        }
    }


    /**
     *场所管理
     */
    public function site_manage()
    {
        $place_service = Service::instance()->find('operation', 'place');//场所接口
        $area_service = Service::instance()->find('operation', 'area');//区域接口
        $companies_service = Service::instance()->find('operation', 'companies');//单位接口
        $areas_list = $area_service->get_area_config();//区域列表
        $companies_list = $companies_service->get_companies_config();//单位列表
        //性质配置
        $place_type_config = Service::instance()->loadConfig('common', 'common', 'getPlaceTypeConfig');
        if (IS_POST) {
            $request = I('request.');
            $where = 1;
            if ($request['keyword']) {
                $key_type = $request['key_type'];
                $keyword = (string)$request['keyword'];
                if ($key_type == 1) {
                    $where .= " and (code like '%$keyword%' or code ='$keyword')";
                } elseif ($key_type == 2) {
                    $where .= " and name like '%" . $keyword . "%'or address like '%$keyword%'";
                } else {
                    $where .= " and code like '%$keyword%' or name like '%$keyword%' or address like '%$keyword%' or district like '%$keyword%' or subdistrict like '%$keyword%'";
                }
            }
            if ($request['place_type']) {
                $place_type = $request['place_type'];
                $where .= " and place_type ='$place_type'";
            }
            if ($request['company_id']) {
                $company_id = $request['company_id'];
                $where .= " and company_id ='$company_id'";
            }
            $site_list = $place_service->get_place_list($where);
            $list = [];
            if ($site_list['count'] > 0) {
                foreach ($site_list['list'] as $value) {
                    $value['num'] = '000' . $value['place_id'];
                    $value['area_id'] = $areas_list[$value['area_id']];
                    $value['company_id'] = $companies_list[$value['company_id']];
                    $value['place_type'] = $place_type_config[$value['place_type']];
                    $value['has_local_server'] = $value['has_local_server'] == 'yes' ? '是' : '否';
                    $list[] = $value;
                }
            }
            $data = array('total' => $site_list['count'], 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->assign('place_type_config', $place_type_config);
            $this->assign('companies_list', $companies_list);
            $this->display('site_manage');
        }
    }

    /**
     * 场所管理员 roleid = 6
     *设置场所信息
     */
    public function set_site_info()
    {
        $admin_db = D('Usap/Admin/Admin');//地区管理员
        $manager_list = $admin_db->where(['roleid' => 6])->getField('userid,username');
        $place_service = Service::instance()->find('operation', 'place');//场所接口
        $area_service = Service::instance()->find('operation', 'area');//区域接口
        $companies_service = Service::instance()->find('operation', 'companies');//单位接口
        //场所性质配置
        $place_type_config = Service::instance()->loadConfig('common', 'common', 'getPlaceTypeConfig');
        $areas_list = $area_service->get_area_config();//区域列表
        $companies_list = $companies_service->get_companies_config();//单位列表
        $id = I('request.id');
        $siteinfo = [];
        if (!empty($id)) {
            $siteinfo = $place_service->getPlaceInfoById($id);
        }
        if (!empty($siteinfo)) {
            $arr = explode('-', $siteinfo['district']);
            unset($siteinfo['district']);
            $siteinfo['district']['province'] = $arr[0];
            $siteinfo['district']['city'] = $arr[1];
            $siteinfo['district']['county'] = $arr[2];
        } else {
            $siteinfo['district']['province'] = '省';
            $siteinfo['district']['city'] = '市';
            $siteinfo['district']['county'] = '地区';
        }
        if (IS_POST) {
            $request = I('request.');
            $district = '';
            if (isset($request['district']) && $request['district']) {
                $arr = [];
                foreach ($request['district'] as $v) {
                    if ($v) {
                        $arr[] = $v;
                    }
                }
                $district = implode('-', $arr);
            }
            if (!$id) {
                $lastid = $place_service->get_place_id();//获取主键
                $code = 'P100' . ($lastid + 1);
            } else {
                $code = 'P100' . $id;
            }
            $data = [
                'area_id' => $request['area_id'],//区域ID
                'company_id' => $request['company_id'],//单位ID
                'name' => $request['name'],//单位名称
                'code' => $code,//单位编号
                'address' => $request['address'],//单位地址
                'district' => $district,//行政区域
                'subdistrict' => $request['subdistrict'],//街道办
                'position' => $request['position'] ? $request['position'] : '',//街道办
                'areas' => $request['areas'],//面积
                'place_type' => $request['place_type'],//场所性质
                'user_id' => $request['user_id'],//管理员
                'manager' => $request['manager'],//负责人
                'manager_phone' => $request['manager_phone'],//手机号码
                'control_room_phone' => $request['control_room_phone'],//控制室电话
                'has_local_server' => $request['has_local_server'],//独立主机
                'created_time' => date('Y-m-d H:i:s')
            ];
            if ($id) {
                $res = $place_service->update_place_info($id, $data);
            } else {
                $res = $place_service->add_place($data);
            }
            if ($res) {
                $data = ['code' => '0000', 'info' => '操作成功'];

            } else {
                $data = ['code' => '-10000', 'info' => '系统错误'];
            }
            $this->ajaxReturn($data);
        } else {
            $map_url = C('API_URL') . '/index.html';
            $this->assign('map_url', $map_url);
            $this->assign('areas_list', $areas_list);
            $this->assign('cp_list', $companies_list);
            $this->assign('siteinfo', $siteinfo);
            $this->assign('manager_list', $manager_list);
            $this->assign('place_type_config', $place_type_config);
            $this->display('set_site_info');
        }
    }

    /**
     * 场所删除
     */
    public function delete_site_info()
    {
        $place_service = Service::instance()->find('operation', 'place');
        $device_service = Service::instance()->find('devices', 'devices');
        $id = I('request.id');
        if (!is_numeric($id)) {
            $id = explode(',', $id);
        }
        if (is_array($id)) {
            $msg = '';
            foreach ($id as $v) {
                $condition = "place_id = '$v'";
                $device_info = $device_service->getDeviceByCondition($condition);
                $place_info = $place_service->getPlaceInfoById($v);
                if ($device_info) {
                    $msg .= $place_info['name'] . '-';
                    continue;
                } else {
                    $place_service->delete_place_info($v);
                }
            }
            if ($msg) {
                $msg .= '删除失败,存在绑定设备';
            }
            $this->success('删除成功' . $msg);
        } else {
            $condition = "place_id = '$id'";
            $device_info = $device_service->getDeviceByCondition($condition);
            if ($device_info) {
                $this->error('删除失败，存在绑定设备信息');
            }
            $place_info = $place_service->getPlaceInfoById($id);
            if ($place_info) {
                $res = $place_service->delete_place_info($id);
                if ($res) {
                    $this->success('删除成功');
                }
            } else {
                $this->error('系统错误');
            }
        }
    }

    /**
     * 运维日志/安装
     * 安装员 roleid =2 跟数据库角色走 现在是巡检员
     */
    public function logs($page = 1, $rows = 20, $order = 'id', $sort = 'asc')
    {
        $admin_db = D('Usap/Admin/Admin');//地区管理员
        $manager_list = $admin_db->getField('userid,username');
        $devices_service = Service::instance()->find('devices', 'devices');
        if (IS_POST) {
            $request = I('request.');
            $where = 1;
            $device_list = $devices_service->getDeviceList($where, $page, $rows, $order = 'created_time', $sort = 'asc');
            $list = [];
            if ($device_list['count'] > 0) {
                foreach ($device_list['list'] as $value) {
                    $res['codeserial'] = $value['codeserial'];
                    $res['device_address'] = $value['device_address'];
                    $res['user'] = $manager_list[$value['user_id']];
                    $res['created_time'] = $value['created_time'];
                    $list[] = $res;
                }
            }
            $data = array('total' => 5, 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->display('logs');
        }

    }

    /**
     * 运维日志/巡检
     */
    public function check_logs()
    {
        $list = [
            [
                'id' => '1',
                'num' => '123456',
                'area' => '123456',
                'cycle' => '123456',
            ],
            [
                'id' => '2',
                'num' => '1323456',
                'area' => '123456',
                'cycle' => '123456',
            ],
        ];
        $data = array('total' => 5, 'rows' => $list);
        $this->ajaxReturn($data);
    }


    /**
     * 获取市、地区信息
     */
    public function getCityCountyInfo()
    {

        $common_service = Service::instance()->find('common', 'common');
        //省份配置
        $name = I('request.name') ? I('request.name') : '';
        $level = I('request.level') ? I('request.level') : '1';
        $RegionList = $common_service->getRegionList($name, $level);
        if ($level == 3 && empty($RegionList)) {
            $RegionList[$name] = $name;
        }
//        dprint($RegionList);
        foreach ($RegionList as $key => $value) {
            $res['id'] = $value;
            $res['name'] = $value;
            $data[] = $res;
        }
        $this->ajaxReturn($data);
    }


    public function Upload()
    {
        $path = array();
        if ($_FILES['file']['name']) {
            $uploadPath = UPLOAD_PATH . 'admin/img/';
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 1048576 * 3;// 3M
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub = false;    //是否自动生成子目录
            $upload->subName = array('date', 'Ymd');
            $upload->saveName = array('uniqid', '');
            $upload->rootPath = $uploadPath; // 设置附件上传根目录
            $info = $upload->upload($_FILES);
            if (!$info) {// 上传错误提示错误信息
                return array('code' => '1001', 'code_str' => $upload->getError());
            } else {// 上传成功 获取上传文件信息
                foreach ($info as $key => $value) {
                    $path[$key] = str_replace(UPLOAD_PATH, '/', $uploadPath) . $value['savepath'] . $value['savename'];
                }
                return array('code' => '0000', 'info' => $path);
            }
        }
        return array();
    }

}
